﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using log4net;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;
using Exchange.Business;
namespace liveatedu
{
    public partial class OtlookCalendar : System.Web.UI.UserControl
    {
        private hnt_ExchangeWebService exchange = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            exchange = new hnt_ExchangeWebService();
            if (CookieHelper.CookieExists("UserOutlookMail"))
            {

                loaddata();
            }
            else {
                pnlCalendar.Visible = false;
            }
        }

        private void loaddata()
        {
            if (Session["Service"] != null)
            {
                ExchangeService service = (ExchangeService)Session["Service"];
                loadCalendar(service);

            }
            else
            {
                ExchangeService service = GetService();
                loadCalendar(service);
                Session["Service"] = service;

            }


        }

        private ExchangeService GetService()
        {
            ExchangeService service = null;
            try
            {
                string email = "";
                string password = "";

                string userEmail = CookieHelper.GetSecureCookieValue("UserOutlookMail");
                string[] strUser = userEmail.Split(',');
                email = strUser[0];
                password = strUser[1];

                service = exchange.CreateExchangeService(email, password);
            }
            catch (Exception) { }

            return service;
        }

        private void loadCalendar(ExchangeService service)
        {

            try
            {
                //string email = "";
                //string password = "";

                //string userEmail = CookieHelper.GetSecureCookieValue("UserOutlookMail");
                //string[] strUser = userEmail.Split(',');
                //email = strUser[0];
                //password = strUser[1];

                //ExchangeService service = exchange.CreateExchangeService(email, password);

                DataTable dtCalendarToday = exchange.GetCalendarToday(service);
                DataTable dtCalendar = exchange.GetCalendarOneWeek(service);

                loadCalendarToday(dtCalendarToday);
                loadCalendarFuture(dtCalendar);


                //dtvCalendar = new DataView(dtCalendar);
                //Session["TableCalendar"] = dtCalendar;
                //Session["TableCalendarOneDay"] = dtCalendarOneDay;

            }
            catch (Exception)
            {
                //Session.Remove("TableCalendar");
                //Session.Remove("TableCalendarOneDay");
            }


        }

        private void loadCalendarToday(DataTable dtCalendarToDay)
        {
            divCalendarToday.Visible = true;
            dtlBlogCategory.DataSource = dtCalendarToDay;
            dtlBlogCategory.DataBind();
            if (dtCalendarToDay.Rows.Count > 0)
            {
                //hplCalendarToday.Visible = true;
                lblNoCalendar.Visible = false;

            }
            else
            {
                lblNoCalendar.Visible = true;
                //hplCalendarToday.Visible = false;
            }
        }

        private void loadCalendarFuture(DataTable dtCalendarFuture)
        {

            grvCalendar.DataSource = dtCalendarFuture;
            grvCalendar.DataBind();

            if (dtCalendarFuture.Rows.Count > 0)
            {
                divCalendarFuture.Visible = true;
            }
            else
            {
                divCalendarFuture.Visible = false;
            }
        }

        protected string FormatHeading(string strHeading)
        {
            string strNew = "";
            strNew = UIHelper.CreateExcerpt(strHeading, 60, " ...");
            return strNew;
        }
    }
}